package com.open.capacity.company.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.common.exception.service.ServiceException;
import com.open.capacity.common.model.SysRecruitment;
import com.open.capacity.common.util.PageUtil;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.common.web.Result;
import com.open.capacity.company.dao.SysRecruitmentDao;
import com.open.capacity.company.service.SysRecruitmentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.apache.ibatis.annotations.Options;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Map;
@Slf4j
@Service
public class SysRecruitmentServiceImpl implements SysRecruitmentService {

    @Autowired
    private SysRecruitmentDao sysRecruitmentDao;



    @Autowired(required = false)
    private TokenStore redisTokenStore;

    @Override
    public PageResult<SysRecruitment> findRecruitment(Map<String, Object> params) throws ServiceException {
        try {
//            int total = sysRecruitmentDao.count(params);
//            List<SysRecruitment> list = Collections.emptyList();
//            if (total > 0) {
//                PageUtil.pageParamConver(params, true);
//                list = sysRecruitmentDao.findRecruitment(params);
//            }
//            return PageResult.<SysRecruitment>builder().data(list).code(0).count((long) total).build();
//        }catch (Exception e){
//            throw new ServiceException(e);
//        }
        if (MapUtils.getInteger(params, "page")!=null && MapUtils.getInteger(params, "limit")!=null){
            PageHelper.startPage(MapUtils.getInteger(params, "page"),MapUtils.getInteger(params, "limit"),true);
        }
        List<SysRecruitment> list =  sysRecruitmentDao.findRecruitment(params);
        PageInfo<SysRecruitment> pageInfo = new PageInfo(list);

        return PageResult.<SysRecruitment>builder().data(pageInfo.getList()).code(0).count(pageInfo.getTotal()).build()  ;
    } catch (Exception e) {
        throw new ServiceException(e);
    }
    }

    @Override
    public Result updateEnabled(Map<String, Object> params) {
        try {
            Long id = MapUtils.getLong(params, "id");
            Boolean enabled = MapUtils.getBoolean(params, "enabled");

            SysRecruitment appRecruitment = sysRecruitmentDao.findById(id);
            if (appRecruitment == null) {
                return Result.failed("用户不存在");
                //throw new IllegalArgumentException("用户不存在");
            }
            appRecruitment.setEnabled(enabled);
//			appCompany.setUpdateTime(new Date());

            int i = sysRecruitmentDao.updateByPrimaryKey(appRecruitment);
            log.info("修改公司：{}", appRecruitment);

            return i > 0 ? Result.succeed(appRecruitment, "更新成功") : Result.failed("更新失败");
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }
//
//    public static void main(String[] args) {
//        @Transactional
//         public boolean createRecruitment(Recruitment REC){
//            {
//                id:
//                employeecount:
//                cost：{
//                   id:
//                    remark:
//                    cost:
//                }
//            }
//            recDao.insertCost(cost);
//            recDao.(rec,cost.getId())
//        }
//        @Options(useGeneratedKeys = "id",keyProperty = "")
//         @insert("insert...")
//        inserCost()
//    }
    @Override
    @Transactional
    public Result saveOrUpdate(SysRecruitment sysRecruitment) throws ServiceException {
        try {



            sysRecruitment.setEnabled(Boolean.TRUE);

//            SysRecruitment.setCreateTime(new Date());

            int i = 0;
//
            if (sysRecruitment.getId() == null) {
//                SysRecruitment p = sysRecruitmentDao.findByRecruitment(sysRecruitment.getCompanyId());
//                if (p != null && p.getId() != null) {
//                    //throw new IllegalArgumentException("用户名已存在");
//                    return Result.failed("用户名已存在");
//                }

                i = sysRecruitmentDao.insert(sysRecruitment);


            } else {

                i = sysRecruitmentDao.updateByPrimaryKey(sysRecruitment);
            }

            return i > 0 ? Result.succeed(sysRecruitment, "操作成功") : Result.failed("操作失败");
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }

    @Override
    public void delete(Long id) {
        try {
            SysRecruitment sysRecruitment = sysRecruitmentDao.findById(id);

            sysRecruitmentDao.deleteById(id);


            log.info("删除角色：{}", sysRecruitment);
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }
}
